package com.whl.sm.common.handler;

import com.whl.sm.common.exception.SMException;
import com.whl.sm.common.result.Result;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice(basePackages = {"com.whl.sm"})
public class GlobalExceptionHandler {


    //Shiro拒绝访问
    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseBody
    public Result error(UnauthenticatedException e){
        e.printStackTrace();
        return Result.build(401,"请进行登录!");
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    /**
     * 自定义异常处理方法
     * @param e
     * @return
     */
    @ExceptionHandler(SMException.class)
    @ResponseBody
    public Result error(SMException e){
        return Result.build(e.getCode(), e.getMessage());
    }
}
